package cn.merryyou.generator.controller;


import cn.merryyou.generator.base.BaseController;
import cn.merryyou.generator.domain.TableInfo;
import cn.merryyou.generator.dto.ResponseDto;
import cn.merryyou.generator.service.GenInfo;
import cn.merryyou.generator.service.IGenService;
import cn.merryyou.generator.text.Convert;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 代码生成 操作处理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/tool/gen")
public class GenController extends BaseController {
    private String prefix = "tool/gen";

    @Autowired
    private IGenService genService;

    @GetMapping()
    public String gen() {
        return redirect("index");
    }

    @RequestMapping("/list/{tableName}")
    @ResponseBody
    public ResponseDto<List<TableInfo>> list(@PathVariable("tableName") String tableName) {
        TableInfo tableInfo = new TableInfo();
        tableInfo.setTableName(tableName);
        startPage();
        List<TableInfo> list = genService.selectTableList(tableInfo);
        return success(list);
    }

    @PostMapping("/list")
    @ResponseBody
    public ResponseDto<List<TableInfo>> list(TableInfo tableInfo) {
        startPage();
        List<TableInfo> list = genService.selectTableList(tableInfo);
        return getDataTable(list);
    }

    /**
     * 生成代码
     */
    @GetMapping("/genCode/{tableName}")
    public void genCode(HttpServletResponse response, @PathVariable("tableName") String tableName, GenInfo genInfo) throws IOException {
        byte[] data = genService.generatorCode(genInfo,tableName);
        response.reset();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyMMdd-HHmm");
        String format = tableName + "_" + simpleDateFormat.format(new Date());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + format + ".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }

    /**
     * 批量生成代码
     */
    @GetMapping("/batchGenCode")
    @ResponseBody
    public void batchGenCode(HttpServletResponse response, String tables, GenInfo genInfo) throws IOException {
        String[] tableNames = Convert.toStrArray(tables);
        byte[] data = genService.generatorCode(genInfo,tableNames);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"ffcs.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}
